package com.cl.edu.web.admin;


import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.cl.common.CrudActionSupport;
import com.cl.core.orm.Page;
import com.cl.edu.entity.Links;
import com.cl.edu.service.admin.LinksManager;

public class LinkAction extends CrudActionSupport<Links>{
	@Autowired
	private LinksManager linksManager;
	
	private Links entity;
	private Integer id;
	
	private Page<Links> page  = new Page<Links>();
	
	@Override
	public String list() throws Exception {
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = linksManager.getLinkPage(page);
		return SUCCESS;
	}
	
	@Override
	public String delete() throws Exception {
		try {
			linksManager.deleteLink(id);
			addActionMessage("删除外部链接成功");
		} catch (Exception e) {
			addActionMessage("删除外部链接失败");
		}
		return list();
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		if(id==null){
			linksManager.addLink(entity);
			addActionMessage("增加外部链接成功");
		} else {
			linksManager.updateLink(entity);
			addActionMessage("更新外部链接成功");
		}

		return list();
		
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if(id == null) {
			entity = new Links();
		}else {
			entity = linksManager.getLink(id);
		}
	}

	@Override
	public Links getModel() {
		return entity;
	}

	public Links getEntity() {
		return entity;
	}

	public void setEntity(Links entity) {
		this.entity = entity;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Page<Links> getPage() {
		return page;
	}

	public void setPage(Page<Links> page) {
		this.page = page;
	}

}
